<?php
class New_informations extends Admin_Controller
{
	
	function __construct()
	{
		parent::__construct();
	}
	
	function index()
	{
		$data['informations'] = new New_information();
		if(@$_GET['search'])$data['informations']->where("title like '%".$_GET['search']."%'");
		if(@$_GET['status'])$data['informations']->where('status',$_GET['status']);
		if(@$_GET['category_id'])$data['informations']->where("category_id = ".$_GET['category_id']);
		$data['informations']->order_by('id','desc')->get_page();
		$this->template->append_metadata(js_lightbox());
		$this->template->append_metadata(js_checkbox('approve'));
		$this->template->build('admin/index',$data);
	}
	
	function form($id=FALSE)
	{
		$data['information'] = new New_information($id);
		$this->template->append_metadata(js_datepicker());
		$this->template->build('admin/form',$data);
	}
	
	function save($id=FALSE)
	{
		if($_POST)
		{
			$information = new New_information($id);
			if($_POST['index_show'] == "")$_POST['index_show'] = "yes";
			if($_POST['tumbon_show'] == "")$_POST['tumbon_show'] = "";
			if($_POST['pormor_show'] == "")$_POST['pormor_show'] = "";
			if($_POST['stat_show'] == "")$_POST['stat_show'] = "";
			if(!$id)$_POST['user_id'] = $this->session->userdata('id');
			$_POST['start_date'] = Date2DB($_POST['start_date']);
			$_POST['end_date'] = Date2DB($_POST['end_date']);
			if($_FILES['image']['name'])
			{
				if($id)$information->delete_file($information->id,'uploads/information/thumbnail','image');
				$information->image = $information->upload($_FILES['image'],'uploads/information/');
			}
			$information->from_array($_POST);
			$information->save();
			set_notify('success', lang('save_data_complete'));
		}
		redirect($_POST['referer']);
	}
	
	function approve($id)
	{
		if($_POST)
		{
			$information = new New_information($id);
			$_POST['approve_id'] = $this->session->userdata('id');
			$information->approve_date = date("Y-m-d H:i:s");
			$information->from_array($_POST);
			$information->save();
			echo approve_comment($information);
		}

	}
	
	function delete($id=FALSE)
	{
		if($id)
		{
			$information = new New_information($id);
			$information->delete();
			set_notify('success', lang('delete_data_complete'));
		}
		redirect($_SERVER['HTTP_REFERER']);
	}
}
?>